// -----------------------------------------------------------------------
// <copyright file="Profile.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using AdCom.Utility;
    using VDC.COMMON;
    
    /// <summary>
    /// Class Profile
    /// </summary>
    public class Profile
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Profile" /> class
        /// </summary>
        public Profile()
        {
            this.ProfileId = 0;
            this.ProfileStatusId = 0;
            this.ProfileTypeId = 0;
            this.LevelId = 0;
            this.StorageLocationProfileListId = 0;
            this.CreateUserId = 0;
            this.Code = string.Empty;
            this.Name = string.Empty;
            this.SmsGet = string.Empty;
            this.Email = string.Empty;
            this.CreateDate = null;
            this.ReceivingDate = null;
            this.AppointmentDate = null;
            this.Object = false;
            this.SettlementDate = null;
            this.Notes = string.Empty;
            this.Attachments = string.Empty;
            this.ContentWork = string.Empty;
            this.IsDelete = false;
            this.OrganName = string.Empty;
            this.Address = string.Empty;
            this.Phone = string.Empty;
        }
        
        /// <summary>
        /// Gets or sets ProfileId
        /// </summary>
        public int ProfileId { get; set; }

        /// <summary>
        /// Gets or sets ProfileStatusId
        /// </summary>
        public int ProfileStatusId { get; set; }

        /// <summary>
        /// Gets or sets ProfileTypeId
        /// </summary>
        public int ProfileTypeId { get; set; }

        /// <summary>
        /// Gets or sets LevelId
        /// </summary>
        public int LevelId { get; set; }

        /// <summary>
        /// Gets or sets StorageLocationProfileListId
        /// </summary>
        public int StorageLocationProfileListId { get; set; }

        /// <summary>
        /// Gets or sets CreateUserId
        /// </summary>
        public int CreateUserId { get; set; }

        /// <summary>
        /// Gets or sets Code
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// Gets or sets Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets SmsGet
        /// </summary>
        public string SmsGet { get; set; }

        /// <summary>
        /// Gets or sets Email
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// Gets or sets CreateDate
        /// </summary>
        public DateTime? CreateDate { get; set; }

        /// <summary>
        /// Gets or sets ReceivingDate
        /// </summary>
        public DateTime? ReceivingDate { get; set; }

        /// <summary>
        /// Gets or sets AppointmentDate
        /// </summary>
        public DateTime? AppointmentDate { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether Object true or false
        /// </summary>
        public bool Object { get; set; }

        /// <summary>
        /// Gets or sets SettlementDate
        /// </summary>
        public DateTime? SettlementDate { get; set; }

        /// <summary>
        /// Gets or sets Notes
        /// </summary>
        public string Notes { get; set; }

        /// <summary>
        /// Gets or sets Attachments
        /// </summary>
        public string Attachments { get; set; }

        /// <summary>
        /// Gets or sets ContentWork
        /// </summary>
        public string ContentWork { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether delete is true or false
        /// </summary>
        public bool IsDelete { get; set; }

        /// <summary>
        /// Gets or sets OrganName
        /// </summary>
        public string OrganName { get; set; }

        /// <summary>
        /// Gets or sets Address
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// Gets or sets Phone
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// Gets Level
        /// </summary>
        public Level Level
        {
            get
            {
                try
                {
                    Level level = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneLevelGetById, this.LevelId));

                    while (reader.Read())
                    {
                        level = new Level();

                        if (!reader.IsDBNull(0))
                        {
                            level.LevelId = reader.GetInt16(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            level.Name = reader.GetString(1);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return level;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets ProfileStatus
        /// </summary>
        public ProfileStatus ProfileStatus
        {
            get
            {
                try
                {
                    ProfileStatus profileStatus = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneProfileStatusGetById, this.ProfileStatusId));

                    while (reader.Read())
                    {
                        profileStatus = new ProfileStatus();

                        if (!reader.IsDBNull(0))
                        {
                            profileStatus.ProfileStatusId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            profileStatus.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            profileStatus.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            profileStatus.Orders = reader.GetInt16(3);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return profileStatus;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets ProfileType
        /// </summary>
        public ProfileType ProfileType
        {
            get
            {
                try
                {
                    ProfileType profileType = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneProfileTypeGetById, this.ProfileTypeId));

                    while (reader.Read())
                    {
                        profileType = new ProfileType();

                        if (!reader.IsDBNull(0))
                        {
                            profileType.ProfileTypeId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            profileType.WorkflowId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            profileType.DepartmentId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            profileType.ProfileTypeGroupId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            profileType.ImplementationListId = reader.GetInt32(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            profileType.ResultsImplementationListId = reader.GetInt32(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            profileType.Code = reader.GetString(6);
                        }

                        if (!reader.IsDBNull(7))
                        {
                            profileType.Name = reader.GetString(7);
                        }

                        if (!reader.IsDBNull(8))
                        {
                            profileType.AmountProfile = reader.GetInt32(8);
                        }

                        if (!reader.IsDBNull(9))
                        {
                            profileType.ResolveDays = reader.GetInt32(9);
                        }

                        if (!reader.IsDBNull(10))
                        {
                            profileType.Fees = reader.GetDecimal(10);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return profileType;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets StorageLocationProfileList
        /// </summary>
        public StorageLocationProfileList StorageLocationProfileList
        {
            get
            {
                try
                {
                    StorageLocationProfileList storageLocationProfileList = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneStorageLocationProfileListGetById, this.StorageLocationProfileListId));

                    while (reader.Read())
                    {
                        storageLocationProfileList = new StorageLocationProfileList();

                        if (!reader.IsDBNull(0))
                        {
                            storageLocationProfileList.StorageLocationProfileListId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            storageLocationProfileList.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            storageLocationProfileList.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            storageLocationProfileList.Descriptions = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            storageLocationProfileList.Parent = reader.GetInt32(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return storageLocationProfileList;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Comments
        /// </summary>
        public IList<Comment> Comments
        {
            get
            {
                try
                {
                    IList<Comment> lstComment = new List<Comment>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneCommentsGetByProfileId, this.ProfileId));
                    while (reader.Read())
                    {
                        Comment comment = new Comment();
                        if (!reader.IsDBNull(0))
                        {
                            comment.CommentsId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            comment.ProfileId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            comment.UserId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            comment.Date = reader.GetDateTime(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            comment.Content = reader.GetString(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            comment.Attachments = reader.GetString(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            comment.Role = reader.GetString(6);
                        }

                        lstComment.Add(comment);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstComment;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Documents
        /// </summary>
        public IList<Document> Documents
        {
            get
            {
                try
                {
                    IList<Document> lstDocument = new List<Document>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneDocumentGetByProfileId, this.ProfileId));
                    while (reader.Read())
                    {
                        Document document = new Document();
                        if (!reader.IsDBNull(0))
                        {
                            document.DocumentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            document.ProfileId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            document.UserId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            document.Date = reader.GetDateTime(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            document.Content = reader.GetString(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            document.Attachments = reader.GetString(5);
                        }

                        lstDocument.Add(document);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstDocument;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Fees
        /// </summary>
        public IList<Fee> Fees
        {
            get
            {
                try
                {
                    IList<Fee> lstFee = new List<Fee>();
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneFeesGetByProfileId, this.ProfileId));
                    while (reader.Read())
                    {
                        Fee fee = new Fee();

                        if (!reader.IsDBNull(0))
                        {
                            fee.FeesId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            fee.ProfileId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            fee.ChargeUserId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            fee.ChargeData = reader.GetDateTime(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            fee.Fees = reader.GetDecimal(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            fee.Note = reader.GetString(5);
                        }

                        lstFee.Add(fee);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstFee;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets ProfileApproves
        /// </summary>
        public IList<ProfileApprove> ProfileApproves
        {
            get
            {
                try
                {
                    IList<ProfileApprove> lstProfileApprove = new List<ProfileApprove>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneProfileApproveGetByProfileId, this.ProfileId));
                    while (reader.Read())
                    {
                        ProfileApprove profileApprove = new ProfileApprove();
                        if (!reader.IsDBNull(0))
                        {
                            profileApprove.ProfileApproveId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            profileApprove.ProfileId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            profileApprove.UserId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            profileApprove.ProfileStatusId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            profileApprove.ApproveDate = reader.GetDateTime(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            profileApprove.Reason = reader.GetString(5);
                        }

                        lstProfileApprove.Add(profileApprove);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstProfileApprove;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets ProfileCompositionProfileLists
        /// </summary>
        public IList<ProfileCompositionProfileList> ProfileCompositionProfileLists
        {
            get
            {
                try
                {
                    IList<ProfileCompositionProfileList> lstProfileCompositionProfileList = new List<ProfileCompositionProfileList>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneProfileCompositionProfileListGetByProfileId, this.ProfileId));
                    while (reader.Read())
                    {
                        ProfileCompositionProfileList profileCompositionProfileList = new ProfileCompositionProfileList();
                        if (!reader.IsDBNull(0))
                        {
                            profileCompositionProfileList.ProfileCompositionProfileListId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            profileCompositionProfileList.ProfileId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            profileCompositionProfileList.CompositionProfileListId = reader.GetInt32(2);
                        }

                        lstProfileCompositionProfileList.Add(profileCompositionProfileList);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstProfileCompositionProfileList;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Works
        /// </summary>
        public IList<Work> Works
        {
            get
            {
                try
                {
                    IList<Work> lstWork = new List<Work>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneWorkGetByProfileId, this.ProfileId));
                    while (reader.Read())
                    {
                        Work work = new Work();
                        if (!reader.IsDBNull(0))
                        {
                            work.WorkId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            work.ProfileId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            work.NodeId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            work.UserId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            work.Date = reader.GetDateTime(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            work.Content = reader.GetString(5);
                        }

                        lstWork.Add(work);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstWork;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets WorkflowProfiles
        /// </summary>
        public IList<WorkflowProfile> WorkflowProfiles
        {
            get
            {
                try
                {
                    IList<WorkflowProfile> lstWorkflowProfile = new List<WorkflowProfile>();
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneWorkflowProfileGetByProfileId, this.ProfileId));
                    while (reader.Read())
                    {
                        WorkflowProfile workflowProfile = new WorkflowProfile();

                        if (!reader.IsDBNull(0))
                        {
                            workflowProfile.WorkflowProfileId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            workflowProfile.ProfileId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            workflowProfile.WorkflowId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            workflowProfile.NodeId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            workflowProfile.SendUserId = reader.GetInt32(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            workflowProfile.MainUserId = reader.GetInt32(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            workflowProfile.ProgressWorkId = reader.GetInt32(6);
                        }

                        if (!reader.IsDBNull(7))
                        {
                            workflowProfile.IsCurrent = reader.GetBoolean(7);
                        }

                        if (!reader.IsDBNull(8))
                        {
                            workflowProfile.StartDate = reader.GetDateTime(8);
                        }

                        if (!reader.IsDBNull(9))
                        {
                            workflowProfile.EndDate = reader.GetDateTime(9);
                        }

                        if (!reader.IsDBNull(10))
                        {
                            workflowProfile.Role = reader.GetString(10);
                        }

                        if (!reader.IsDBNull(11))
                        {
                            workflowProfile.RW = reader.GetBoolean(11);
                        }

                        lstWorkflowProfile.Add(workflowProfile);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstWorkflowProfile;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}
